<template>
    <!-- 结构  左-右(上下) -->
    <Layout>
        <!-- 左  tree -->
        <Sider hide-trigger>
            <Tree :data="treeList" ref="tree" @on-select-change ="onSelectChange"></Tree>
        </Sider>
        <Layout>
            <!-- 结构  上下 -->
            <Header >
                <!-- 上  上传控件 upload -->
                 <Upload
                    multiple
                    ref='upload'
                    name='avatar'
                    type="drag"
                    :data="uploadData"
                    show-upload-list: false
                    action="http://localhost:3000/upload/upload"
                    :before-upload='beforeUpload'>
                    <div style="padding: 20px 0">
                        <Icon type="ios-cloud-upload" size="52" style="color: #3399ff"></Icon>
                        <p>Click or drag files here to upload</p>
                    </div>
                </Upload>
            </Header>
            <!-- 下 Grid -->
            <Content>
                <Table border :columns="columns" :data="list"></Table>
            </Content>
        </Layout>
    </Layout>
</template>
<script>
import formBase from "../../common/formBase";
import modalBase from "../../common/modalBase";
import treeBase from "../../common/treeBase";
import moment from 'moment';
export default {
    mixins: [formBase,modalBase,treeBase],
    data() {
        return {
            columns: [
                {
                    type: 'selection',
                    width: 60,
                    align: 'center'
                },
                {
                    title: '用户名',
                    key: 'fieldname',
                    width: 150,
                    
                },
                {
                    title: '图片',
                    key: 'filename'
                },
                {
                    title: '上传日期',
                    width: 150,
                    key: 'date',
                    render: (h, params) => {
                    return h('div', [
                        h('data',moment(params.row.date).format('YYYY-MM-DD HH:mm:ss'))
                    ]);
                    }
                },
                {
                    title: '数据操作',
                    key: 'action',
                    align: 'center',
                    width: 150,
                    render: (h, params) => {
                        return h('div', [
                            h('Button', {
                                props: {
                                    type: 'primary',
                                    size: 'small'
                                },
                                style: {
                                    marginRight: '5px'
                                },
                                on: {
                                    click: () => {
                                        this.formState= true;
                                        for (const key in this.formValidate) {
                                            if(key == 'interest') {
                                                this.formValidate[key] = params.row[key].split(',');
                                            } else {
                                                this.formValidate[key] = params.row[key];
                                            }
                                        }
                                    }
                                }
                            }, '修改'),
                            h('Button', {
                                props: {
                                    type: 'error',
                                    size: 'small'
                                },
                                on: {
                                    click: () => {
                                        console.log(params)
                                            this.remove(params.index)
                                            this.axios({
                                                url:`http://localhost:3000/users/data/${params.row._id}`,
                                                method:'delete'
                                            }).then(res=>{
                                                this.$Message.info('删除成功');
                                                this.getDate();
                                            })
                                    }
                                }
                            }, '删除')
                        ]);
                    }
                }
            ],
        }
    },
    methods: {
        //获取tree被选中的节点的对象
        onSelectChange() {
            let selectedObj = this.$refs.tree.getSelectedNodes();
            if(selectedObj[0]){
                this.treeSelectedObj = selectedObj[0];
                this.getData({cateId: this.treeSelectedObj._id});
            }
        },
        //upload 上传判断
        beforeUpload() {
            // console.log(this.uploadData)
            if(!this.uploadData.paths) {
                this.$Modal.info({
                    title: '提示',
                    content: '请选择分类'
                });
                //返回false  停止上传
                return false;
            }
        }
    },
    mounted() {
        this.getTreeData();
    },
    computed: {
        //上传额外数据
        uploadData() {
            let newData = {};
            if(this.treeSelectedObj && (this.treeSelectedObj.path || this.treeSelectedObj._id)){
                newData.paths = `${this.treeSelectedObj.path},${this.treeSelectedObj._id}`;
                newData.cateId = this.treeSelectedObj._id;
            }
            return newData;
        }
    }
}
</script>

<style scoped>
 .ivu-layout-header,
 .ivu-layout-sider {
     background: #f5f7f9;
 }

 .ivu-layout.ivu-layout-has-sider {
     height: 100%;
 }

 .ivu-layout-header {
     height: 180px;
     padding: 0;
 }

 .ivu-upload {
     height: 100%;
 }
</style>

